package com.whf.android.jar.net.rx;

import androidx.annotation.NonNull;

import com.whf.android.jar.net.RestClientBuilder;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.IFailure;
import com.whf.android.jar.net.callback.rx.IRxSuccess;

import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created
 *
 * @author qf
 * @version 2017/4/2
 */

public final class RxRestClientBuilder<T> {

    private final WeakHashMap<String, Object> PARAMS = new WeakHashMap<>();
    private final WeakHashMap<String, Object> HEADERS = new WeakHashMap<>();
    private String mUrl = null;
    private RequestBody mBody = null;
    private IRxSuccess<T> mSuccess = null;
    private IFailure mFailure = null;
    private IError mError = null;

    RxRestClientBuilder() {
    }

    public final RxRestClientBuilder<T> url(String url) {
        this.mUrl = url;
        return this;
    }

    public final RxRestClientBuilder<T> headers(WeakHashMap<String, Object> headers) {
        HEADERS.putAll(headers);
        return this;
    }

    public final RxRestClientBuilder<T> headers(String key, Object value) {
        HEADERS.put(key, value);
        return this;
    }

    public final RxRestClientBuilder<T> params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    public final RxRestClientBuilder<T> params(String key, Object value) {
        PARAMS.put(key, value);
        return this;
    }

    public final RxRestClientBuilder<T> raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }


    public final RxRestClientBuilder<T> success(IRxSuccess<T> iSuccess) {
        this.mSuccess = iSuccess;
        return this;
    }

    public final RxRestClientBuilder<T> failure(IFailure iFailure) {
        this.mFailure = iFailure;
        return this;
    }

    public final RxRestClientBuilder<T> error(IError iError) {
        this.mError = iError;
        return this;
    }

    @NonNull
    public final RxRestClient<T> build() {
        return new RxRestClient<>(mUrl, PARAMS, HEADERS, mBody, mSuccess, mFailure, mError);
    }
}
